package com.lhuc.utils;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.lhuc.pojo.User;

import java.util.List;

public class JsonUtil {


    public static void main(String[] args) {

        /**
         * String转JSONArray
         */
        String str = "[{\"name\":\"Tim\",\"age\":\"25\",\"sex\":\"male\"},{\"name\":\"Tom\",\"age\":\"28\",\"sex\":\"male\"},{\"name\":\"Lily\",\"age\":\"15\",\"sex\":\"female\"}]";
        JSONArray tableData = JSONArray.parseArray(str);

        for(int i=0;i<tableData.size();i++) {
            System.err.println(tableData.getJSONObject(i));
        }


        /**
         * JSONArray转List
         */
        //JSONArray array = new JSONArray();
        List<User> list = JSONObject.parseArray(tableData.toJSONString(), User.class);
        list.forEach(System.err::println);


        /**
         * List转JSONArray
         */
        //List<T> list = new ArrayList<T>();
        JSONArray array = JSONArray.parseArray(JSON.toJSONString(list));
        System.err.println(array);
    }

}
